﻿using System;
using Storm.TextEditor;
using Storm.TextEditor.Editor.CodeCompletion;
using Storm.TextEditor.Editor.Text;
using Moonlite.ExtensionApi.GlobalInterfaces;

namespace Moonlite.Platform.Text.File
{
    public class SnippetCompletionData : ICompletionData
    {
        private int imageIndex;
        private string text;
        private string description;
        private object tag;
        private ISnippet snippet;
        public ISnippet Snippet
        {
            get { return snippet; }
            set { snippet = value; }
        }

        public bool CustomInsertionAction
        {
            get { return true; }
        }

        public int ImageIndex
        {
            get { return imageIndex; }
            set { imageIndex = value; }
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public object Tag
        {
            get { return tag; }
            set { tag = value; }
        }

        public void Insert(int x, int y, TextEditor textEditor)
        {
            int length = 0;
            string[] lines = snippet.Content.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            int index = 0;
            int startY = textEditor.Caret.Position.Y;
            string indentationPattern = "";
            if (textEditor.KeepTabs == true)
            {
                indentationPattern = new string('\t', textEditor.Caret.CurrentRow.Depth);
            }
            else
            {
                indentationPattern = new string(' ', textEditor.TabSpaces * textEditor.Caret.CurrentRow.Depth);
            }

            foreach (string line in lines)
            {
                textEditor.Document.InsertText((index > 1 ? indentationPattern : "") + line + (index < lines.Length - 1 ? Environment.NewLine : ""),
                    index == 0 ? textEditor.Caret.Position.X - 1 : 1, startY + index);
                length += (index > 0 ? indentationPattern.Length : 0) + line.Length + Environment.NewLine.Length;
                index++;
            }

            int newOffset = textEditor.Caret.Offset + length;
            textEditor.Caret.Position = textEditor.Document.IntPosToPoint(newOffset);
            textEditor.Caret.CropPosition();
        }
        public bool ShouldInsert(char key)
        {
            if (key == '\t')
            {
                return true;
            }

            return false;
        }

        public SnippetCompletionData()
        {
        }

        public SnippetCompletionData(int imageIndex, string text, string description)
        {
            this.ImageIndex = imageIndex;
            this.Text = text;
            this.Description = description;
        }
    }
}
